package com.danger.widget;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.AppCompatSeekBar;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.danger.activity.MainActivity;
import com.danger.activity.R;
import com.danger.service.PlayServiceImpl;
import com.danger.util.Constant;

/**
 * Created by 俊 on 2016/8/16.
 */
public class PlayerPopupWindow extends PopupWindow implements View.OnClickListener {

    private int width;
    private int height;
    private View view;
    private Context context;
    private LinearLayout play_layout;
    private RelativeLayout root_layout;
    private Toolbar player_toolbar;
    private MarqueeTextView name_textView;
    private MarqueeTextView artist_textView;
    private MyCircleImageView photo_imageView;
    private TextView left_textView;
    private AppCompatSeekBar seekBar;
    private TextView right_textView;
    private ImageButton previous_song_button;
    private ImageButton play_song_button;
    private ImageButton next_song_button;

    public PlayerPopupWindow(Context context, LinearLayout play_layout) {
        calWidthAndHeight(context);

        view = LayoutInflater.from(context).inflate(R.layout.player_layout, null);
        this.context = context;
        this.play_layout = play_layout;

        setContentView(view);
        setWidth(width);
        setHeight(height);
        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(false);

        bindView();
        bindEvent();
    }

    private void calWidthAndHeight(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(metrics);

        width = metrics.widthPixels;
        height = metrics.heightPixels;
    }

    private void bindView() {
        root_layout = (RelativeLayout) view.findViewById(R.id.root_layout);
        player_toolbar = (Toolbar) view.findViewById(R.id.player_toolbar);
        name_textView = (MarqueeTextView) view.findViewById(R.id.name_textView);
        artist_textView = (MarqueeTextView) view.findViewById(R.id.artist_textView);
        photo_imageView = (MyCircleImageView) view.findViewById(R.id.photo_imageView);
        left_textView = (TextView) view.findViewById(R.id.left_textView);
        seekBar = (AppCompatSeekBar) view.findViewById(R.id.seekBar);
        right_textView = (TextView) view.findViewById(R.id.right_textView);
        previous_song_button = (ImageButton) view.findViewById(R.id.previous_song_button);
        play_song_button = (ImageButton) view.findViewById(R.id.play_song_button);
        next_song_button = (ImageButton) view.findViewById(R.id.next_song_button);
    }

    private void bindEvent() {
        player_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                play_layout.setVisibility(View.VISIBLE);
            }
        });
        previous_song_button.setOnClickListener(this);
        play_song_button.setOnClickListener(this);
        next_song_button.setOnClickListener(this);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    Intent intent = new Intent(context, PlayServiceImpl.class);
                    intent.putExtra("progress", progress);
                    intent.setAction(Constant.DRAG_SEEKBAR);
                    context.startService(intent);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(context, PlayServiceImpl.class);
        switch (v.getId()) {
            case R.id.previous_song_button:
                intent.setAction(Constant.PREVIOUS_MSG);
                context.startService(intent);
                break;
            case R.id.play_song_button:
                intent.setAction(Constant.PAUSE_MSG);
                context.startService(intent);
                break;
            case R.id.next_song_button:
                intent.setAction(Constant.NEXT_MSG);
                context.startService(intent);
                break;
            default:
                break;
        }
    }

    public RelativeLayout getRootLayout() {
        return root_layout;
    }

    public TextView getNameTextView() {
        return name_textView;
    }

    public TextView getArtistTextView() {
        return artist_textView;
    }

    public MyCircleImageView getPhotoImageView() {
        return photo_imageView;
    }

    public ImageButton getPlaySongButton() {
        return play_song_button;
    }

    public AppCompatSeekBar getSeekBar() {
        return seekBar;
    }

    public TextView getLeftTextView() {
        return left_textView;
    }

    public TextView getRightTextView() {
        return right_textView;
    }
}
